<?php
namespace app\wap\controller;

class Comment extends Base{

	// 评论提交
	public function submit(){
		$this->user_id();

		$model = model('Comment');
		$post_data['reply_id']    = $this->_post('reply_id', '', 0);
		$post_data['object_id']   = $this->_post('object_id', '评论对象ID不能为空！');
		$post_data['content']     = htmlspecialchars($this->_post('content', '评论内容不能为空！'));
		$post_data['user_id']     = $this->_user_id;
		$post_data['module']      = $this->_post('module', '', 'ask_answer');
		$post_data['create_time'] = time();
		if($model->create($post_data, true) !== false){
			$comment_id = $model->getLastInsID();
			$info = $model->find($comment_id);
			$list = $info ? array($info) : array();

			// $this->assign('list', $list);
			// $data['html']       = $this->fetch('index_ajax_item');
			$list['user_name'] 		 = model('User')->getShowNameAttr($info['user_id']);
			$list['time_ago_desc']   = model('User')->getTimeAgoDescAttr($info['create_time']);
			$list['reply_user_name'] = model('Comment')->getReplyUserNameAttr($info['reply_user_id']);;
			$data['data'] = $list;
			// 消息推送
			if($post_data['reply_id']){
				model('Message')->comment_push($comment_id, 'answer_reply', $this->_user_id);
			}else{
				model('Message')->comment_push($comment_id, 'answer', $this->_user_id);
			}

			$this->success('评论成功', '', $data);
		} else {
			$this->error('评论失败！');
		}
	}
	// AJAX获取列表数据
	public function ajax_list(){
		$module    = $this->_get('module', '', 'ask_answer');
		$object_id = $this->_get('object_id', '评论对象ID不能为空！');

		$where['module']    = $module;
		$where['object_id'] = $object_id;
		$list  = model('Comment')->where($where)->order('comment_id desc')->select();

		$this->assign('list', $list);
		$data['html'] = $this->fetch('index_ajax_item');

		$this->success('获取评论成功！', '', $data);
	}
	// 删除
	public function delete(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$comment_id = $this->_get('comment_id', '对不起，您的操作有误！');
		$model      = model('Comment');
		$info       = $model->where(array('comment_id' => $comment_id))->find();
		if(!$info){
			$this->error('此评论不存在！');
		}
		if($info['user_id'] != $this->_user_id){
			$this->error('对不起，您没有权限操作！');
		}
		$module    = $info['module'];
		$object_id = $info['object_id'];

		// 删除评论
		$model->where(array('comment_id' => $comment_id))->delete();
		$comment_num = $model->updateCommentNum($object_id, $module);

		$data['label'] = $comment_num;
		$this->success('删除成功!', '', $data);
	}
}